from local.processor import write_json
import pprint
import pandas as pd

from en2zh import *
from feature import platform2text_col


def get_data(x, col_1):
    """

    :param x:
    :param col_1: xxx.xxx.xxx的形式 ， 所以要用split
    :return:
    """
    title = ''
    for key in col_1.split('.'):
        if title == '':
            print(x.keys())
            if key in x:
                # 如果有字段， 就取对应的值
                title = x[key]
            else:
                # 没有就直接退出
                title = ''
                break
        else:
            if key in title:
                title = title[key]
            else:
                title = ''
                break
    return title
import os

c = 'search_items'
from local.processor import *
from nlp import *
from feature import get_wenzhen_row
for plat in platform2text_col:
    #  获取平台的中文名
    plat_zh = mapping_d.get(plat, plat)
    #  获取collection的中文
    coll_zh = mapping_d.get(c, c)
    # 生成过了就不要重复生成了
    if os.path.exists(f'df2/{coll_zh}_{plat_zh}.xlsx'):
        continue
    col_1 = platform2text_col[plat]['title']
    col_2 = platform2text_col[plat]['content']
    pd_platrows = []
    pk_data = read_cache(f'wenzhen/{plat}.pk')
    for x in pk_data:
        row  = get_wenzhen_row(plat,  x)
        if row:
            pd_platrows.append(row)
        else:
            if plat in platform2text_col:
                title, content = '', ''
                #print(plat, col_1, col_2)
                # 获取title 和content。 要注意下title = 空的情况
                if col_1 != '?':
                    title = get_data(x, col_1)
                if col_2 != '?':
                    content = get_data(x, col_2)
                pd_platrows.append((title, content, x['search_key']))
    if len(pd_platrows[-1])>3:
        df = pd.DataFrame(pd_platrows, columns=['平台','搜索词','标题','内容','病人','医生','对话'])
    else:
        df = pd.DataFrame(pd_platrows, columns=['标题', '内容', '搜索词'])
    # 去掉一些乱码，不然不能保存
    df = df.applymap(clean_illegal_characters)

    df['平台'] = plat_zh
    # 保存df
    df.to_excel(f'df2/{coll_zh}_{plat_zh}.xlsx')